import time
from PyQt5.QtGui import QPalette, QFont, QPixmap, QIcon
from PyQt5.QtCore import Qt, QTimer, QDateTime
from PyQt5.QtWidgets import *
import sys
import frozen_dir
import Simulation
from Form1 import Form1
from Form2 import Form2
from Form3 import Form3
from Form4 import Form4
from Setting_UI import Setting_UI
import Py_Vissim.StartSimulation as StartSimulation
import threading
from lib.db import database
import loadin


class TextEditDemo(QWidget, Simulation.Simulation_cfg):
    def __init__(self, parent=None):
        super(TextEditDemo, self).__init__(parent)
        self.setWindowTitle("公交优先控制策略评估系统")
        dir = frozen_dir.app_path() + r'\pic\bjtu.jpg'
        self.setWindowIcon(QIcon(dir))
        self.resize(1580, 880)

        # 主界面样式
        qssStyle = '''
                    background-color:#87CEFA;
                    '''
        self.setStyleSheet(qssStyle)

        # 设置lcd时钟
        self.Timer = QTimer()  # 自定义QTimer
        self.Timer.start(500)  # 每0.5秒运行一次
        self.Timer.timeout.connect(self.updateTime)  # 连接updateTime

        # 创建堆叠按钮
        self.btnPress1 = QPushButton("可视\n地图")
        self.btnPress1.setFixedSize(90, 60)
        self.btnPress2 = QPushButton("数据\n表格")
        self.btnPress2.setFixedSize(90, 60)
        self.btnPress3 = QPushButton("优化\n结果")
        self.btnPress3.setFixedSize(90, 60)
        self.btnPress4 = QPushButton("参数\n设置")
        self.btnPress4.setFixedSize(90, 60)
        self.btnPress5 = QPushButton("开始\n仿真")
        self.btnPress5.setFixedSize(90, 60)
        self.btnPress6 = QPushButton("结束\n仿真")
        self.btnPress6.setFixedSize(90, 60)
        self.btnPress6.setEnabled(False)

        btn_qssStyle = ''' 
                            QPushButton{
                            text-align : center;
                            font-family:Microsoft Yahei;
                            font-weight: bold;
                            font-size:12pt;
                            background-color : #F5F5F5;
                            border-color: gray;
                            border-width: 2px;
                            border-radius: 10px;
                            padding: 1px;
                            height : 14px;
                            border-style: outset;
                            }
                            QPushButton:pressed{
                            text-align : center;
                            font-family:Microsoft Yahei;
                            font-weight: bold;
                            font-size:12pt;
                            background-color : #5F9EA0;
                            border-color: gray;
                            border-width: 2px;
                            border-radius: 10px;
                            padding: 1px;
                            height : 14px;
                            border-style: outset;
                            }
                        '''
        self.btnPress1.setStyleSheet(btn_qssStyle)
        self.btnPress2.setStyleSheet(btn_qssStyle)
        self.btnPress3.setStyleSheet(btn_qssStyle)
        self.btnPress4.setStyleSheet(btn_qssStyle)
        self.btnPress5.setStyleSheet(btn_qssStyle)
        self.btnPress6.setStyleSheet(btn_qssStyle)

        # 添加页面
        self.form1 = Form1()
        self.form2 = Form2()
        self.form3 = Form3()
        self.form4 = Form4()
        self.set_ui = Setting_UI()

        # <editor-fold desc="设置LOGO">
        self.head_img_lable = QLabel(self)
        self.head_img_lable.setMaximumSize(60, 60)  # 调整logo大小
        dir = frozen_dir.app_path() + r'\pic\bjtu.jpg'
        head_pic = QPixmap(dir)
        self.head_img_lable.setPixmap(head_pic)
        self.head_img_lable.setScaledContents(True)  # 图片自适应LABEL大小
        # </editor-fold>

        # <editor-fold desc="设置标题">
        self.head_name_label = QLabel(self)
        self.head_name_label.setText('公交优先控制策略及其评估系统')
        self.head_name_label.setStyleSheet("""
        font-family:Microsoft Yahei;
        font-size: 26pt;   
        color: #8470FF;
        font-weight: bold;
        """)
        self.head_name_label.setAutoFillBackground(True)  # 设置背景充满，为设置背景颜色的必要条件

        self.head_name_label.resize(40, 40)
        # </editor-fold>

        # <editor-fold desc="设置LCD">
        self.lcd_time = QLCDNumber(self)
        self.lcd_time.setDigitCount(10)  # 初始显示
        self.lcd_time.setDigitCount(20)  # 位数限制
        self.lcd_time.setSegmentStyle(QLCDNumber.Flat)  # 内部填充
        self.lcd_time.setStyleSheet("""
                            text-align : center;
                            font-family:Microsoft Yahei;
                            font-weight: bold;
                            font-size:24pt;
                            background-color : #B0C4DE;
                            border-color: gray;
                            border-width: 2px;
                            border-radius: 10px;
                            padding: 1px;
                            height : 14px;
                            border-style: outset;""")  # 样式表单
        # </editor-fold>

        # <editor-fold desc="添加堆叠窗口">
        self.widget = QWidget()
        self.stacked_layout = QStackedLayout()
        self.widget.setLayout(self.stacked_layout)
        self.widget.setStyleSheet("""
        background-color : #F0FFFF;
        """)
        self.stacked_layout.addWidget(self.form1)
        self.stacked_layout.addWidget(self.form2)
        self.stacked_layout.addWidget(self.form3)
        self.stacked_layout.addWidget(self.form4)
        self.stacked_layout.setCurrentIndex(1)
        # </editor-fold>

        # <editor-fold desc="添加head">
        head_layout = QHBoxLayout()  # 添加水平布局
        head_layout.addWidget(self.head_img_lable)
        head_layout.addWidget(self.head_name_label)
        head_layout.addWidget(self.btnPress1)
        head_layout.addWidget(self.btnPress2)
        head_layout.addWidget(self.btnPress3)
        head_layout.addWidget(self.btnPress4)
        head_layout.addWidget(self.btnPress5)
        head_layout.addWidget(self.btnPress6)
        head_layout.addStretch(7)  # 添加弹簧
        head_layout.addWidget(self.lcd_time)
        layout = QVBoxLayout()  # 添加垂直布局
        layout.addItem(head_layout)
        layout.addWidget(self.widget)
        # </editor-fold>

        self.setLayout(layout)
        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)
        self.btnPress3.clicked.connect(self.btnPress3_Clicked)
        self.btnPress4.clicked.connect(self.btnPress4_Clicked)
        self.btnPress5.clicked.connect(self.btnPress5_Clicked)
        self.btnPress6.clicked.connect(self.btnPress6_Clicked)

    def updateTime(self):
        self.lcd_time.display(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss'))  # 显示时间的格式

    def btnPress1_Clicked(self):
        self.btnPress1.setDown(True)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(False)
        self.stacked_layout.setCurrentIndex(1)

    def btnPress2_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(True)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(False)
        self.stacked_layout.setCurrentIndex(2)

    def btnPress3_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(True)
        self.btnPress4.setDown(False)
        self.stacked_layout.setCurrentIndex(0)

    def btnPress4_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(True)
        # self.stacked_layout.setCurrentIndex(3)
        child_window = self.set_ui
        child_window.exec()

    def btnPress5_Clicked(self):
        """
        开始运行仿真
        """
        if self.set_ui.Remote_VISSIM is False:
            self.th_vissim = threading.Thread(target=StartSimulation.StartSimulation_Click,
                                              args=(self.Flag_Vissim_Road,))
            self.th_vissim.start()
            self.form3.Flag_run_vissim = True
            self.btnPress5.setEnabled(False)
            self.btnPress6.setEnabled(True)
        else:
            print("尝试远程连接VISSIM")
            try:
                self.set_ui.Remote_Client.tcp_client_start()
                msg = {'TYPE': 'run'}
                self.set_ui.Remote_Client.tcp_send(mess=msg)
                time.sleep(2)
                self.set_ui.Remote_Client.tcp_close()
            except:
                pass
            else:
                self.form3.Flag_run_vissim = True
                self.btnPress5.setEnabled(False)
                self.btnPress6.setEnabled(True)

    def btnPress6_Clicked(self):
        """
        结束仿真
        """
        self.form3.Flag_run_vissim = False
        self.btnPress5.setEnabled(True)
        self.btnPress6.setEnabled(False)


if __name__ == "__main__":

    try:
        db = database()
        db.Init_DB()
        db.OC_Filling()
    except:
        pass
    else:
        app = QApplication(sys.argv)
        win = TextEditDemo()
        win_lodin = loadin.LoginForm()
        win_lodin.show()
        sys.exit(app.exec_())
